/**
 *
 */
package idao.engine.parameter;

import idao.DaoRuntimeException;
import ognl.Ognl;
import ognl.OgnlException;

/**
 * A parameter backed by OGNL expression to define which property will be used as
 * parameter value.
 * 
 * @author huhao
 */
class OgnlParameter extends SqlParameter
{

    private Object expression;

    public OgnlParameter(int parameterIndex, String parameterExpression)
    {
        super(parameterIndex, parameterExpression);
        try {
            expression = Ognl.parseExpression(parameterExpression);
        } catch (OgnlException e) {
            throw new DaoRuntimeException("Invalid OGNL expression '" + parameterExpression + "'.",
                            e);
        }
    }

    @Override
    protected Object getParameterValue(Object[] args)
    {
        try {
            return Ognl.getValue(expression, args[0]);
        } catch (OgnlException e) {
            throw new DaoRuntimeException("Failed to get parameter value.", e);
        }
    }
}
